package com.itwn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itwn.entity.Role;
import com.itwn.mapper.RoleMapper;
import com.itwn.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @topic:
 **/
@Service
public class RoleService {
    @Autowired
    RoleMapper roleMapper;

    public RoleVo findRole(Integer roleId){
        RoleVo roleVo = roleMapper.selectRoleVo(roleId);
        return roleVo;
    }

    public PageInfo<Role> findAll(String roleName, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Role> roles = roleMapper.selectAllByName(roleName);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return pageInfo;
    }

    @Transactional
    public int addRole(RoleVo roleVo){
        int insert = roleMapper.insert(roleVo);
        Integer roleId = roleVo.getRoleId();
        int i = roleMapper.insertRolePermission(roleId, roleVo.getPerList(), roleVo.getInsertTime());
        return insert;
    }

    public RoleVo findRoleVo(Integer roleId){
        RoleVo roleVo = roleMapper.selectRoleVo(roleId);
        return roleVo;
    }

    @Transactional
    public int removeRoleAndPermission(Integer roleId){
        int delete = roleMapper.deleteByPrimaryKey(roleId);
        roleMapper.deleteRolePermission(roleId);
        return delete ;
    }

    @Transactional
    public int modifyRoveAndPermission(RoleVo roleVo){
        int i = roleMapper.updateRole(roleVo);
        roleMapper.deleteRolePermission(roleVo.getRoleId());
        roleMapper.insertRolePermission(roleVo.getRoleId(),roleVo.getPerList(),roleVo.getUpdateTime());
        return i;
    }

    public List<Role> queryAllRole() {
        List<Role> roles = roleMapper.selectAll();
        System.out.println(roles);
        return roles;
    }
}
